package org.beans.expense.common.page;

import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.beans.expense.common.CommonConstants;
import org.beans.expense.common.domain.DailyExpense;
import org.beans.expense.common.service.IDailyExpenseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author: arunachalam
 * @since: 2/16/13
 */
public class AddEditDailyExpensePage extends AbstractBasePage
{
    private static final Logger logger = LoggerFactory.getLogger(AddEditDailyExpensePage.class);
    private IDailyExpenseService dailyExpenseService = (IDailyExpenseService) new ClassPathXmlApplicationContext(
            CommonConstants.CONTEXT_CONFIG_FILE).getBean("dailyExpenseService");
    //@SpringBean
    //private IDailyExpenseService dailyExpenseService;
    private DailyExpense dailyExpense;

    public AddEditDailyExpensePage(DailyExpense dailyExpense)
    {
        super();
        this.dailyExpense = dailyExpense;
        add(createPageHeader());
        add(expenseForm());
        add(createBackNavigationLink());
    }

    Label createPageHeader()
    {
        String header = "add";
        if (isEditMode()) {
            header = "edit";
        }
        return new Label("header", getString(header));
    }


    private Form<DailyExpense> expenseForm()
    {
        Form<DailyExpense> form = new Form<DailyExpense>("addEditForm", new CompoundPropertyModel<DailyExpense>(dailyExpense));
        form.add(createDescriptionField());
        form.add(createAmountField());
        form.add(createDateField());
        form.add(createSaveChangesButton());
        return form;
    }

    private TextField<String> createDescriptionField()
    {
        TextField<String> descriptionField = new TextField<String>("description");
        descriptionField.setRequired(true);
        return descriptionField;
    }

    private TextField createAmountField()
    {
        TextField amountField = new TextField("amount");
        amountField.setRequired(true);
        return amountField;
    }

    private DateTimeField createDateField()
    {
        DateTimeField dateTimeField = new DateTimeField("dateTime");
        /*{
            @Override
            protected DatePicker newDatePicker()
            {
                return new DatePicker()
                {
                    @Override
                    protected String getDatePattern()
                    {
                        return "dd/MM/yyyy";
                    }
                };
            }
        };*/
        dateTimeField.setRequired(true);
        return dateTimeField;
    }

    private Button createSaveChangesButton()
    {
        return new Button("saveButton")
        {
            @Override
            public void onSubmit()
            {
                if (!isEditMode()) {
                    addEntity();
                } else {
                    updateEntity();
                }
            }
        };
    }

    private Link createBackNavigationLink()
    {
        return new Link("backNavigationLink")
        {
            @Override
            public void onClick()
            {
                setResponsePage(DailyExpensesPage.class);
            }
        };
    }

    private boolean isEditMode()
    {
        return dailyExpense.getId() > 0;
    }

    private void addEntity()
    {
//        try {
        dailyExpenseService.create(dailyExpense);
//        } catch (Exception ex) {
//            logger.debug("Error occurred while adding entity ", ex);
//        }
    }

    private void updateEntity()
    {
        try {
            dailyExpenseService.update(dailyExpense);
        } catch (Exception ex) {
            logger.debug("Error occurred while updating entity ", ex);
        }
    }
}
